import { IAppOption } from "../../../../typings";
import { perfectUserProfile } from "../../../utils/api";

const app = getApp<IAppOption>()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        redirectUrl: ""
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options: Record<string, string | undefined>) {
        if(options.redirectUrl) {
            this.data.redirectUrl = options.redirectUrl
        }
    },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
      
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.hideHomeButton();
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
      
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
      
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(_pts): WechatMiniprogram.Page.ICustomShareContent {
        return {}
    },
    commit(options: any) : boolean {
        let errorMsg = "";
        if(options.detail.value.username == "") {
            errorMsg = "账号不能为空"
        }
        if(errorMsg == "" && (options.detail.value.username.length < 6 || options.detail.value.username.length > 20)) {
            errorMsg = "账号长度限制6位到20位"
        }
        if(errorMsg == "" && options.detail.value.password == "") {
            errorMsg = "密码不能为空"
        }
        if(errorMsg == "" && (options.detail.value.password.length < 6 || options.detail.value.password.length > 20)) {
            errorMsg = "密码长度限制6位到20位"
        }
        if(errorMsg == "" && options.detail.value.confirm_password == "") {
            errorMsg = "确认密码不能为空"
        }
        if(errorMsg == "" && options.detail.value.confirm_password != options.detail.value.password) {
            errorMsg = "两次输入密码不一致"
        }
        console.log(options)
        if(errorMsg != "") {
            wx.showToast({
                title: errorMsg,
                icon: "none"
            })
            return false;
        }
        let p = perfectUserProfile({
            username: options.detail.value.username,
            password: options.detail.value.password
        })

        p.then((res: any) => {
            app.globalData.environment!.userInfo!.username = res.username
            let userInfo = wx.getStorageSync("userInfo")
            if(userInfo) {
                userInfo.username = res.username
                wx.setStorageSync("userInfo", userInfo)
            }
            if(this.data.redirectUrl) {
                let that = this;
                wx.navigateTo({
                    url: this.data.redirectUrl,
                    fail: function() {
                        wx.switchTab({
                            url: that.data.redirectUrl
                        })
                    }
                })
            } else {
                wx.switchTab({
                    url: "/pages/user/index"
                })
            }
        }).catch(e => {
            wx.showToast({
                title: e,
                icon: "none"
            })
            return false;
        })

        return true;
    }
  })
  
  // 解决无法重新声明块级范围变量的问题
  export {}